package com.iseastar.dianxiaosan.home.parcel;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.adapter.ParcelAbnormalAdapter;
import com.iseastar.dianxiaosan.model.ParcelAbnormalResultBean;
import com.iseastar.guojiang.BaseActivity;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.SListView;

/**
 * 异常包裹处理
 *
 * @author HuXinDan<br>
 *         2016-11-28下午6:02:36
 */
public class ParcelAbnormalActivity extends BaseActivity2 {
    private SListView mListView;
    private TextView parcelAbnormalNum;// 异常包裹数量
    private ParcelAbnormalAdapter mAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UserBean user = AppCache.getUser();
        AppHttp.getInstance().stationParcelAbnormalAlls(user.getStationId());
    }

    protected void findViewById() {
        setContentView(R.layout.parcel_abnormal);
        super.findViewById();
        getAppTitle().setCommonTitle("异常包裹");

        mListView = (SListView) findViewById(R.id.listview);
        parcelAbnormalNum = (TextView) findViewById(R.id.parcel_abnormal_num);// 待取走包裹数量
        mListView.setFooterDividersEnabled(true);
        mAdapter = new ParcelAbnormalAdapter(null, getContext(), mListView);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 获取驿站点下面的订单列表（异常包裹）
            case MsgID.parcel_abnormal_alls:
                onRefreshComplete();
                final ReqResult<ParcelAbnormalResultBean> result = JSON.parser(msg.obj, ParcelAbnormalResultBean.class);
                if (checkLoginStatus(result) && result.getResult() != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ParcelAbnormalResultBean items = result.getResult();
                            mAdapter.setItems(items.getParcelList(), true);
                            if (mAdapter.isEmpty()) {
                                findViewById(R.id.parcel_abnormal_empty_layout).setVisibility(View.VISIBLE);
                                findViewById(R.id.listview).setVisibility(View.GONE);
                                findViewById(R.id.parcel_abnormal_num).setVisibility(View.GONE);
                            } else {
                                parcelAbnormalNum.setText("共有" + mAdapter.getItemsSize() + "个异常包裹等待处理");
                                findViewById(R.id.parcel_abnormal_empty_layout).setVisibility(View.GONE);
                                findViewById(R.id.listview).setVisibility(View.VISIBLE);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
